﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using Sunyi.Project.WebPoint.DataAccess;

using ActivitySystem.DataContract;


namespace ActivitySystem.DataAccess
{
    public partial class ActivityTypeDao
    {
        public Collection<ActivityType> Search(string name, bool isActive, int pageIndex, int pageSize)
        {
            Collection<ActivityType> activityTypes = new Collection<ActivityType>();

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_Search";
            command.Parameters.Add(new SqlParameter("@Name", name));
            command.Parameters.Add(new SqlParameter("@IsActive", isActive));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        activityTypes.Add(CreateEntityByReader(reader));
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return activityTypes;
        }

        public int SearchCount(string name, bool isActive)
        {
            int count = 0;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_SearchCount";
            command.Parameters.Add(new SqlParameter("@Name", name));
            command.Parameters.Add(new SqlParameter("@IsActive", isActive));

            try
            {
                connection.Open();

                count = (int)command.ExecuteScalar();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return count;
        }

        public ActivityType SelectByName(string name)
        {
            ActivityType activityType = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_SelectByName";
            command.Parameters.Add(new SqlParameter("@Name", name));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        activityType = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return activityType;
        }

        public Collection<ActivityType> SelectAll()
        {
            Collection<ActivityType> activityTypes = new Collection<ActivityType>();

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_ActivityType_SelectAll";

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        activityTypes.Add(CreateEntityByReader(reader));
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return activityTypes;
        }
    }
}
